package com.skyline.spot.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.skyline.base.type.Activity;
import com.skyline.base.type.Authority;
import com.skyline.common.bean.Page;
import com.skyline.spot.dao.SpotCharacteristicDao;
import com.skyline.spot.model.SpotCharacteristic;
import com.skyline.spot.service.SpotCharacteristicService;
import com.skyline.wo.dao.AlbumDao;

@Service("spotCharacteristicService")
public class SpotCharacteristicServiceImpl implements SpotCharacteristicService {

	@Autowired
	SpotCharacteristicDao spotCharacteristicDao;
	@Autowired
	AlbumDao albumDao;

	@Override
	public Long addCharacteristic(Long spotId, String spotName,String spotPortrait, Long ownerId,
			String ownerNickname, String ownerPortrait, Long creatorId,
			String creatorNickName, String creatorPortrait,
			String charateristicTile, String charateristicDetail,Integer consume) {
		try {
			Long albumId = albumDao.insertAlbumReturnId(spotId, spotName,
					spotPortrait, charateristicTile, "null",charateristicTile, Authority.PUBLIC,
					Activity.NORMAL, "cover", "jpg");
			return spotCharacteristicDao.insertCharacteristic(spotId, spotName,spotPortrait,
					ownerId, ownerNickname, ownerPortrait, creatorId,
					creatorNickName, creatorPortrait, charateristicTile,
					charateristicDetail,consume, albumId);
		} catch (Exception e) {
			return Long.valueOf(0);
		}

	}

	@Override
	public SpotCharacteristic addCharacteristic(SpotCharacteristic sc) {

		//try {
			Long albumId = albumDao.insertAlbumReturnId(sc.getSpotId(),
					sc.getSpotName(), sc.getSpotPortrait(), sc.getCharacteristicTitle(),
					"null", sc.getCharacteristicTitle(), Authority.PUBLIC, Activity.NORMAL, "cover",
					"jpg");
			Long spotCharaId= spotCharacteristicDao.insertCharacteristic(sc.getSpotId(),
					sc.getSpotName(), sc.getSpotPortrait(), sc.getOwnerId(),
					sc.getOwnerNickname(), sc.getOwnerPortrait(),
					sc.getCreatorId(), sc.getCreatorNickname(),
					sc.getCreatorPortrait(), sc.getCharacteristicTitle(),
					sc.getCharacteristicDetail(),sc.getConsume(), albumId);
			sc.setAlbumId(albumId);
			sc.setId(spotCharaId);
			return sc;
	//	} catch (Exception e) {
	//		return Long.valueOf(0);
	//	}
	}

	@Override
	public void updateCharacteristic(Long id, String charateristicTile,
			String charateristicDetail) {
		spotCharacteristicDao.updateCharacteristic(id, charateristicTile,
				charateristicDetail);

	}

	@Override
	public void updateCharacteristic(Long id, Activity activity) {
		spotCharacteristicDao.updateCharacteristic(id, activity);

	}
	
	@Override
	public void updateCharacteristic(Long id, String portrait) {
		spotCharacteristicDao.updateCharacteristic(id, portrait);
		
	}
	
	@Override
	public SpotCharacteristic votes(Long scId, Integer star) {
		
		SpotCharacteristic sc =spotCharacteristicDao.queryCharacteristicById(scId,Activity.NORMAL);
		
		Integer votes = sc.getVotes();
		sc.setVotes(votes + 1);
		float _star = (sc.getStar()*votes + star) / (votes + 1);
		sc.setStar(_star);
		spotCharacteristicDao.updateSpotCharaStar(scId, _star);
		return sc;

	}

	@Override
	public List<SpotCharacteristic> queryCharacteristicBySpotId(Long spotId,
			Page page, Activity activity) {

		return spotCharacteristicDao.queryCharacteristicBySpotrId(spotId, page,
				activity);
	}

	@Override
	public SpotCharacteristic queryCharacteristicById(Long id, Activity activiy) {

		return spotCharacteristicDao.queryCharacteristicById(id, activiy);
	}





}
